package com.x.game.model;

import com.x.game.enums.Direction;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;


/**
 * 聚合根：一盘游戏的操作历史
 * 对应表：t_operation_history
 */

@Data
public class OperationHistoryAR implements Serializable {

    private Long id;                    // 自增主键
    private String gameId;              // 游戏聚合根ID
    private Integer operateIndex;       // 本局内的操作序号（0,1,2...）
    private Direction direction;        // 枚举：UP/DOWN/LEFT/RIGHT
    private BoardSnapshot boardBefore;  // 滑动前盘面
    private BoardSnapshot boardAfter;   // 滑动后盘面
    private Integer scoreBefore;
    private Integer scoreAfter;
    private Long userId;                // 冗余用户ID，方便审计
    private LocalDateTime operateTime;  // 操作时间
    private Boolean undone = false;     // 软撤销标记

    /* ========== 工厂方法 ========== */
    public static OperationHistoryAR create(String gameId,
                                            Long userId,
                                            Integer index,
                                            Direction direction,
                                            BoardSnapshot before,
                                            BoardSnapshot after,
                                            int scoreBefore,
                                            int scoreAfter) {
        OperationHistoryAR hist = new OperationHistoryAR();
        hist.gameId = gameId;
        hist.userId = userId;
        hist.operateIndex = index;
        hist.direction = direction;
        hist.boardBefore = before;
        hist.boardAfter = after;
        hist.scoreBefore = scoreBefore;
        hist.scoreAfter = scoreAfter;
        hist.operateTime = LocalDateTime.now();
        hist.undone = false;
        return hist;
    }

    /* ========== 领域行为 ========== */
    public void markUndone() {
        this.undone = true;
    }
}
